package liquidfs;

import java.util.concurrent.atomic.*;

import liquidfs.core.*;

public class Context {
   
   private MetadatabaseInterface db;
   private AtomicLong messageNumber = new AtomicLong(0);
   
   public Context (MetadatabaseInterface db) {
      
      this.db = db;
      String value;
      
      // Initialize the message number
      value = db.getField("MessageNumber");
      if (value == null) {
         value = "0";
         db.addField("MessageNumber", value);
      }
      this.messageNumber.set(Long.parseLong(value));
   }
   
   public long getMessageNumber () {
      return messageNumber.get();
   }
   
   public long nextMessageNumber () {
      messageNumber.incrementAndGet();
      db.setField("MessageNumber", messageNumber.toString());
      
      return messageNumber.get();
   }
   
}
